/*
 * GPS_MTK.h
 *
 * Created: 01.05.2012 13:24:49
 *  Author: maxim_karban@gmail.com
 */ 


#ifndef GPS_MTK_H_
#define GPS_MTK_H_

#include <stdbool.h>
#include <stdint.h>

#define GPS_INIT_BAUDRATE 38400UL
#define GPS_NORMAL_BAUDRATE 9600UL
#define FRSKY_NORMAL_BAUDRATE 9600UL

#define UBRR038400 0x000B
#define UBRR09600 0x002F

#define TX_GPS_EN DDRC |= 1 << 0; PORTC |= 1 << 0
#define TX_GPS_DIS DDRC |= 1 << 0; PORTC &= ~(1 << 0)
#define TX_FRSKY_EN DDRD |= 1 << 5; PORTD |= 1 << 5
#define TX_FRSKY_DIS DDRD |= 1 << 5; PORTD &= ~(1 << 5) 

#define ACTIVATE_FRSKY_TX UBRR0 = UBRR09600; TX_GPS_DIS; TX_FRSKY_EN

#define ACTIVATE_GPS_TX_INIT UBRR0 = UBRR038400; TX_FRSKY_DIS; TX_GPS_EN

#define ACTIVATE_GPS_TX_NORMAL UBRR0 = UBRR09600;TX_FRSKY_DIS; TX_GPS_EN

#define GPS_DECODE_BUFFER_SIZE 90
#define GPS_ROUND_BUFFER_SIZE 200

#define GPS_DELAY _delay_ms(700)

extern bool gps_gsa_is_ready;
extern bool gps_gsv_is_ready;
extern bool gps_gga_is_ready;
extern bool gps_rmc_is_ready;

#define gps_sentences_is_ready \
  ( gps_gsa_is_ready & \
    gps_gsv_is_ready & \
    gps_gga_is_ready & \
    gps_rmc_is_ready )


extern uint8_t gps_status;
extern int16_t gps_alt_bp;
extern uint16_t gps_alt_ap;
extern uint8_t gps_alt_measure;
extern int16_t gps_lat_bp;
extern uint16_t gps_lat_ap;
extern uint8_t gps_lat_ns;
extern int16_t gps_lon_bp;
extern uint16_t gps_lon_ap;
extern uint8_t gps_lon_ew;
extern int16_t gps_speed_bp;
extern uint16_t gps_speed_ap;
extern int16_t gps_direction_bp;
extern uint16_t gps_direction_ap;
extern uint8_t gps_cnt;


extern uint8_t GPS_Round_RX_Buffer[];

extern uint8_t GPS_DECODE_Buffer[];

extern uint8_t GPSRXState;

extern bool gps_new_data_available;
extern bool gps_decoder_buffer_is_ready_to_write;
extern bool gps_decoder_buffer_is_ready_to_write;
extern bool gps_decoder_buffer_is_ready_to_read;

extern void InsertCRC(uint8_t * _str);
extern double strtodouble(uint8_t * _str);
extern void strtobpap(uint8_t * _str, int16_t *bp, uint16_t *ap);
extern void gpsMTKReset(void);
extern int ParseRMC(void);
extern int ParseGSV(void);
extern int ParseGSA(void);
extern int ParseGGA(void);
extern int ParseGPS(void);



#endif /* GPS_MTK_H_ */